package com.example.a2032;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.media.AudioManager;
import android.net.wifi.WifiConfiguration;
import android.os.Bundle;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.multidex.MultiDexApplication;

import com.amap.api.fence.GeoFenceClient;
import com.example.a2032.Utils.AesTest;
import com.example.a2032.Utils.GeoFenceUtils;
import com.example.a2032.Utils.LocationUtils;
import com.example.a2032.Utils.RxBus;
import com.example.a2032.Utils.SerialPortUtil;
import com.example.a2032.Utils.SharedPreferenceUtils;
import com.example.a2032.Utils.Utils;
import com.example.a2032.Utils.WebSocketUtils;
import com.example.a2032.Utils.WifiConnectUtils;
import com.example.a2032.db.DataDbHelper;
import com.example.a2032.db.EmDbHelper;
import com.example.a2032.entity.ActionData;
import com.example.a2032.entity.BleData;
import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMOptions;
import com.hyphenate.easeui.EaseIM;
import com.tencent.bugly.crashreport.CrashReport;

public class MyApp extends MultiDexApplication implements Application.ActivityLifecycleCallbacks {
    private static final String TAG = "MyApp";
    private static MyApp instance;
    Activity showActivity;

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        LocationUtils.init(this);
        registerActivityLifecycleCallbacks(this);
        SharedPreferenceUtils.init(this);
        SerialPortUtil.getInstance().init(this);
        DataDbHelper.getInstance(this).initDb();
        WebSocketUtils.init(this);
       // CrashReport.initCrashReport(getApplicationContext(), "d4b2d405fa", false);
     //   CrashReport.setUserId(Utils.getIMEI(this));
       initEM();
        // ClockUtils.getInstance().setNextHeartPackageClock(this);
        setAlarmMode();
        Log.i(TAG, "onCreate: ");
       if (BuildConfig.needWifi) {
            WifiConnectUtils wifiConnectUtils = new WifiConnectUtils(this);
            wifiConnectUtils.connectWifi("TP-LINK_8653", "abc12345@.", WifiConfiguration.SECURITY_TYPE_PSK);
       }
        GeoFenceUtils geoFenceUtils=new GeoFenceUtils(this, GeoFenceClient.GEOFENCE_IN);
       geoFenceUtils.remove("11");

        // Log.i(TAG, "onCreate: "+isLocServiceEnable());
    }

    private void setAlarmMode() {
        int notifyMode = SharedPreferenceUtils.getInteger(SharedPreferenceUtils.KEY_NotifyMode, 2);
        changeNotifyMode(notifyMode);
    }

    public void changeNotifyMode(int notifyMode) {
        Log.i(TAG, "changeNotifyMode: " + notifyMode);
        AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        if (notifyMode == 0) {
            am.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
        } else if (notifyMode == 1) {
            am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
            Settings.System.putInt(getContentResolver(), Settings.System.VIBRATE_WHEN_RINGING, 0);
        } else {
            Settings.System.putInt(getContentResolver(), Settings.System.VIBRATE_WHEN_RINGING, 1);
            am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
            //     am.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_ON);
        }
    }

    public static MyApp getInstance() {
        return instance;
    }

    private void initEM() {
        EMOptions options = new EMOptions();
// 默认添加好友时，是不需要验证的，改成需要验证
        options.setAcceptInvitationAlways(false);
// 是否自动将消息附件上传到环信服务器，默认为True是使用环信服务器上传下载，如果设为 false，需要开发者自己处理附件消息的上传和下载
        options.setAutoTransferMessageAttachments(true);
// 是否自动下载附件类消息的缩略图等，默认为 true 这里和上边这个参数相关联
        options.setAutoDownloadThumbnail(true);
//初始化
        EMClient.getInstance().init(this, options);
//在做打包混淆时，关闭debug模式，避免消耗不必要的资源
        EMClient.getInstance().setDebugMode(true);
        EaseIM.getInstance().init(this, options);
        login();


    }

    public void login() {
        String userName = SharedPreferenceUtils.getSpString(SharedPreferenceUtils.watchId);
        if (TextUtils.isEmpty(userName)) {
            return;
        }
        BleData bleData = new BleData();
        bleData.setAction(ActionData.ActionRegisterFinish);
        RxBus.getInstance().post(bleData);
        EMClient.getInstance().login(userName, "jointcorp#0323", new EMCallBack() {//回调
            @Override
            public void onSuccess() {
                EMClient.getInstance().groupManager().loadAllGroups();
                EMClient.getInstance().chatManager().loadAllConversations();
                Log.d("main", "登录聊天服务器成功！");
               // EmDbHelper.getInstance(instance).initDb(finalUserName);

            }

            @Override
            public void onProgress(int progress, String status) {

            }

            @Override
            public void onError(int code, String message) {
                Log.d("main", "登录聊天服务器失败！" + message);
            }
        });
    }


    @Override
    public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {

    }

    @Override
    public void onActivityStarted(@NonNull Activity activity) {

    }

    @Override
    public void onActivityResumed(@NonNull Activity activity) {
        this.showActivity = activity;

    }

    @Override
    public void onActivityPaused(@NonNull Activity activity) {

    }

    @Override
    public void onActivityStopped(@NonNull Activity activity) {

    }

    @Override
    public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {

    }

    @Override
    public void onActivityDestroyed(@NonNull Activity activity) {

    }

    public Activity getShowActivity() {
        return this.showActivity;
    }

    long goalTipsTime = 0;

    public long getGoalTipsTime() {
        return goalTipsTime;
    }

    public void setGoalTipsTime(long goalTipsTime) {
        this.goalTipsTime = goalTipsTime;
    }


}
